﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;

namespace Synergy
{
    public partial class FormSu : Form
    {
        static bool tags;
        static string PrintName;
        public FormSu()
        {
            InitializeComponent();
        }

        private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void button_search_Click(object sender, EventArgs e)
        {
            System.Data.DataTable tb;
            string sql;
            sql = "";
            if (this.dateTimePicker_jsrq.Checked)
            {
                sql = "";
            }


            tb = pub.U8GetTable(sql);
            this.dataGridView1.DataSource = tb;
            this.dataGridView1.Refresh();

        }

        private void FormSu_Load(object sender, EventArgs e)
        {
            tags = false;
            ComboBoxInitl();
            this.dataGridView1.AutoGenerateColumns = false;
            tags = true;
        }

        private void ComboBoxInitl()
        {

            ///单据编号
            SaleDeliveryInitl();
            ///客商编号
            PartnerInitl();
            ///
            this.dateTimePicker_jsrq.Value = DateTime.Today;
            this.dateTimePicker_ksrq.Value = DateTime.Today;
            this.dateTimePicker_jsrq.Checked = false;
            this.dateTimePicker_ksrq.Checked = false;
        }

        private void SaleDeliveryInitl()
        {
            string sql;
            sql = @"select top 100   ID,code 
 from  SA_SaleDelivery 
where 1=1 

 order by code asc ";
            System.Data.DataTable patb1 = pub.U8GetTable(sql);
            DataRow dr = patb1.NewRow();
            dr["ID"] = 0;
            patb1.Rows.InsertAt(dr, 0);

            System.Data.DataTable patb2 = patb1.Copy();

            this.comboBox_ksbh.DataSource = patb1;
            this.comboBox_ksbh.DisplayMember = "Code";
            this.comboBox_ksbh.ValueMember = "ID";

            this.comboBox_jsbh.DataSource = patb2;
            this.comboBox_jsbh.DisplayMember = "Code";
            this.comboBox_jsbh.ValueMember = "ID";
        }

        private void PartnerInitl()
        {
            string sql;
            sql = @"select  PartnerDTO.partnerType, [PartnerDTO].id as ID,[PartnerDTO].code as Code,[PartnerDTO].code+' '+[PartnerDTO].name as Name,[PartnerType].Name AS PartnerType_Name,[PriceGrade].Name AS PriceGrade_Name,[Saledepartment].name AS Saledepartment_Name,[Saleman].name AS Saleman_Name,[PartnerDTO].aRBalance AS ARBalance,[PartnerDTO].aPBalance AS APBalance,[PartnerDTO].advRBalance AS AdvRBalance,[PartnerDTO].advPBalance AS AdvPBalance,[PartnerDTO].disabled AS Disabled,[PartnerDTO].idpartnerclass AS PartnerDTO_idpartnerClass,[SaleDepartment].id as SaleDepartment_id,[SaleMan].id as SaleMan_id
 from [AA_Partner] [PartnerDTO]
 LEFT OUTER JOIN [AA_Department] [SaleDepartment] on [PartnerDTO].idsaledepartment=[SaleDepartment].id
 LEFT OUTER JOIN [AA_PartnerAddress] [PartnerAddresDTOs] on [PartnerDTO].id=[PartnerAddresDTOs].idpartner
 LEFT OUTER JOIN [AA_Person] [SaleMan] on [PartnerDTO].idsaleman=[SaleMan].id
 LEFT OUTER JOIN [eap_enumitem] [PartnerType] on [PartnerDTO].PartnerType=[PartnerType].id
 LEFT OUTER JOIN [eap_enumitem] [PriceGrade] on [PartnerDTO].PriceGrade=[PriceGrade].id
 where (( 1=1 and (PartnerDTO.partnerType <>  N'211') and (([PartnerAddresDTOs].IsDefaultAddress=1) or ([PartnerAddresDTOs].IsDefaultAddress is null)) )) AND 1=1 AND (PartnerDTO.disabled = 0 ) ORDER BY PartnerDTO.Code ASC";
            System.Data.DataTable pttb1 = pub.U8GetTable(sql);
            DataRow dr = pttb1.NewRow();
            dr["Code"] = 0;
            pttb1.Rows.InsertAt(dr, 0);
            System.Data.DataTable pttb2 = pttb1.Copy();
            this.comboBox_ksks.DataSource = pttb1;
            this.comboBox_ksks.DisplayMember = "Name";
            this.comboBox_ksks.ValueMember = "Code";

            this.comboBox_jsks.DataSource = pttb2;
            this.comboBox_jsks.DisplayMember = "Name";
            this.comboBox_jsks.ValueMember = "Code";
        }

        private void checkBox1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
            {
                this.dataGridView1.Rows[i].Cells[1].Value = this.checkBox1.Checked;
            }
        }

        private void button_print_Click(object sender, EventArgs e)
        {
            if (this.printDialog1.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            PrintName = this.printDialog1.PrinterSettings.PrinterName;

            for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
            {
                if (this.dataGridView1.Rows[i].Cells["C选择"].Value != null)
                {
                    PrintDocument(this.dataGridView1.Rows[i].Cells["C单据编号"].Value.ToString());
                }
            }
        }
        /// <summary> 
        /// 
        /// </summary>
        /// <param name="p"></param>
        private void PrintDocument(string jinhuodanhao)
        {
            string sql;
            int row, rows;

            sql = @"SELECT  存货编号, 存货名称, 物料规格, 仓库编号, 仓库名称, 客商编号, 客商名称, 销货单号, 销货单行,销货单主键, 存货主键, 仓库主键, 客商主键, 主数量, 
                      辅助数量, 批次号, 是否扫描通过,主计量,辅计量,地址,单据日期,制单人,审核人

       FROM   [View_xiaohuodan]";
            sql += " where [销货单号] ='" + jinhuodanhao + "' order by [销货单行] ";
            System.Data.DataTable jhtb = pub.U8GetTable(sql);
            Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
            Workbooks wbks = app.Workbooks;
            _Workbook _wbk = wbks.Add(System.Windows.Forms.Application.StartupPath + "\\SaleDelivery.xlsx");
            app.Visible = true;
            Sheets shs = _wbk.Sheets;
            //shs.Visible = true;

            //i是要取得的sheet的index

            _Worksheet _wsh = (_Worksheet)shs.get_Item(1);
            _wsh.Cells[1, 1] = pub.GetCode128B(jhtb.Rows[0]["销货单号"].ToString());
            _wsh.Cells[3, 3] = jhtb.Rows[0]["销货单号"].ToString();
            _wsh.Cells[3, 7] = jhtb.Rows[0]["销货单号"].ToString();
            _wsh.Cells[2, 10] = jhtb.Rows[0]["单据日期"].ToString();
            _wsh.Cells[3, 10] = jhtb.Rows[0]["制单人"].ToString();
            _wsh.Cells[4, 3] = jhtb.Rows[0]["客商名称"].ToString();
            _wsh.Cells[4, 10] = DateTime.Now.ToString();

            _wsh.Cells[5, 6] = jhtb.Rows[0]["地址"].ToString() + "";

            ///先清空
            ///
            for (int m = 8; m <= 27; m++)
            {
                for (int n = 1; n <= 11; n++)
                {
                    _wsh.Cells[m, n] = "";
                }
            }
            row = 1;
            int page, pages;
            rows = jhtb.Rows.Count;
            pages = rows / 10;
            if (rows > 10 * pages)
            {
                pages++;
            }
            page = 1;
            for (int i = 0; i < jhtb.Rows.Count; i++)
            {
                ///塞值
                _wsh.Cells[2 * row + 6, 1] = (i + 1).ToString();
                _wsh.Cells[2 * row + 6, 2] = jhtb.Rows[i]["仓库编号"].ToString() + "";///仓库编号
                _wsh.Cells[2 * row + 6, 4] = jhtb.Rows[i]["辅助数量"].ToString() + "";///辅助数量
                _wsh.Cells[2 * row + 6, 5] = jhtb.Rows[i]["辅计量"].ToString() + "";///辅计量
                _wsh.Cells[2 * row + 6, 6] = jhtb.Rows[i]["存货编号"].ToString() + "";///商品代码
                _wsh.Cells[2 * row + 7, 6] = jhtb.Rows[i]["存货名称"].ToString() + "";///存货名称
                //////
                _wsh.Cells[2 * row + 6, 7] = pub.GetCode128B(jhtb.Rows[i]["销货单行"].ToString() + "$" + jhtb.Rows[i]["存货编号"].ToString()); ///行号+存货编号
                _wsh.Cells[2 * row + 6, 9] = jhtb.Rows[i]["主数量"].ToString();///主数量
                _wsh.Cells[2 * row + 6, 10] = jhtb.Rows[i]["主计量"].ToString();///主计量
                _wsh.Cells[2 * row + 6, 11] = jhtb.Rows[i]["批次号"].ToString();///批次号


                if (row >= 10)
                {
                    row = 1;
                    _wsh.Cells[1, 9] = "第" + page.ToString() + "页";
                    _wsh.Cells[1, 11] = "共" + pages.ToString() + "页";
                    _wbk.PrintOut(1, 1, 1, false, PrintName, false, false, null);
                    page++;
                    for (int m = 8; m <= 27; m++)
                    {
                        for (int n = 1; n <= 11; n++)
                        {
                            _wsh.Cells[m, n] = "";
                        }
                    }

                }
                else
                {
                    row++;
                }
            }
            if (row > 1)
            {
                _wsh.Cells[1, 9] = "第" + pages.ToString() + "页";
                _wsh.Cells[1, 11] = "共" + pages.ToString() + "页";
                _wbk.PrintOut(1, 1, 1, false, PrintName, false, false, null);
            }
            _wbk.Close(false, null, null);
            app.Quit();

        }
    }
}
